package com.czl.module_service.viewmodel.assetUse

import androidx.databinding.ObservableField
import com.blankj.utilcode.util.GsonUtils
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.tengyun.CategoryBean
import com.czl.base.data.bean.tengyun.ListBean
import com.czl.base.data.bean.tengyun.SortBean
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper

class SortChooseListViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {

    var currentPage = 1
    val uc = UiChangeEvent()
    var searchName = ObservableField("")
    var storehouseId: Int? = null

    class UiChangeEvent {
        val loadCompleteEvent: SingleLiveEvent<ListBean<SortBean>> = SingleLiveEvent()
        val loadCategoryEvent: SingleLiveEvent<List<CategoryBean>> = SingleLiveEvent()
    }

    val onLoadMoreListener: BindingCommand<Void> = BindingCommand(BindingAction {
        if (storehouseId == null) {
            getSortList(sortName = searchName.get())
        } else {
            getStorehouseStockInfo(sortName = searchName.get(), storehouseId = storehouseId)
        }
    })

    val onRefreshListener: BindingCommand<Void> = BindingCommand(BindingAction {
        currentPage = 1
        if (storehouseId == null) {
            getSortList()
        } else {
            getStorehouseStockInfo(storehouseId = storehouseId)
        }
    })

    val search: BindingCommand<Void> = BindingCommand(BindingAction {
        showLoading()
        currentPage = 1
        if (storehouseId == null) {
            getSortList(sortName = searchName.get())
        } else {
            getStorehouseStockInfo(sortName = searchName.get(), storehouseId = storehouseId)
        }
    })

    val selectedCategory: BindingCommand<Void> = BindingCommand(BindingAction {
        getStorehousePropClass()
    })

    fun getSortList(
        priceInfo: Int? = 1,
        companyId: Int? = null,
        sortName: String? = null,
        sortPIdList: List<Int>? = null
    ) {
        model.mTengYunHttpDataSource.getSortList(
            GsonUtils.toJson(
                mapOf(
                    "isPasteCode" to null,
                    "pageNo" to currentPage,
                    "sortName" to sortName,
                    "companyId" to companyId,
                    "priceInfo" to priceInfo,
                    "sortPIdList" to sortPIdList
                )
            )
        )
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<ListBean<SortBean>>>() {
                override fun onResult(t: BaseBean<ListBean<SortBean>>) {
                    if (t.state == 0) {
                        currentPage++
                        uc.loadCompleteEvent.postValue(t.data)
                    }
                    dismissLoading()
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }
            })
    }

    private fun getStorehouseStockInfo(
        sortName: String? = null,
        storehouseId: Int? = null
    ) {
        model.mTengYunHttpDataSource.getStorehouseStockInfo(
            GsonUtils.toJson(
                mapOf(
                    "pageNo" to currentPage,
                    "pageSize" to 10,
                    "sortName" to sortName,
                    "storehouseId" to storehouseId
                )
            )
        )
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<ListBean<SortBean>>>() {
                override fun onResult(t: BaseBean<ListBean<SortBean>>) {
                    if (t.state == 0) {
                        currentPage++
                        uc.loadCompleteEvent.postValue(t.data)
                    }
                    dismissLoading()
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }
            })
    }

    private fun getStorehousePropClass() {
        model.mTengYunHttpDataSource
            .getclassificationlistCompany(
                GsonUtils.toJson(
                    mapOf(
                        "companyId" to model.getLocalUserInfo()?.companyId,
                        "classStatusList" to arrayOf(0, 1, 2)
                    )
                )
            )
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .doOnSubscribe {
                showLoading()
            }
            .subscribe(object : ApiSubscriberHelper<BaseBean<List<CategoryBean>>>() {
                override fun onResult(t: BaseBean<List<CategoryBean>>) {
                    dismissLoading()
                    if (t.errorCode == 0) {
                        t.data?.let {
                            uc.loadCategoryEvent.postValue(it)
                        }
                    }
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }
            })
    }
}